package com.jiangjun.fetch.filter;

import java.io.IOException;
import java.io.StringReader;

import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.parser.XMLDocumentFilter;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.cyberneko.html.HTMLConfiguration;
import org.dom4j.Document;
import org.dom4j.io.DOMReader;

import com.jiangjun.fetch.common.AttributeChain;
import com.jiangjun.fetch.common.ClassifyFilter;
import com.jiangjun.fetch.common.WebContext;

public class HtmlDocumentFilter extends Filter {

	@Override
	public String doProcess(WebContext ctx) throws Exception {
		// TODO Auto-generated method stub
		/**
		 * 转换之前去掉 title 里的',会造成nekohtml在解析html时内存溢出
		 */
		repS(ctx);
		
		DOMReader dr = new DOMReader();
		HTMLConfiguration htmlConfig = new HTMLConfiguration();
		htmlConfig.setFeature(
				"http://cyberneko.org/html/features/balance-tags", true);
		htmlConfig.setProperty(
				"http://cyberneko.org/html/properties/names/elems", "upper");
		htmlConfig.setProperty(
				"http://cyberneko.org/html/properties/names/attrs", "lower");
		htmlConfig.setProperty(
				"http://cyberneko.org/html/properties/default-encoding",
				ctx.getCharset());
		htmlConfig.setFeature("http://xml.org/sax/features/namespaces", false);
		htmlConfig
				.setFeature(
						"http://cyberneko.org/html/features/scanner/script/strip-comment-delims",
						false);
		htmlConfig.setFeature(
				"http://cyberneko.org/html/features/augmentations", true);
		htmlConfig
				.setFeature(
						"http://apache.org/xml/features/scanner/notify-char-refs",
						true);
		htmlConfig.setFeature(
				"http://apache.org/xml/features/scanner/notify-builtin-refs",
				true);
		htmlConfig
				.setFeature(
						"http://cyberneko.org/html/features/scanner/notify-builtin-refs",
						true);
		DOMParser parser = new DOMParser(htmlConfig);

		// 添加管道过滤器
		ClassifyFilter classifyFilter = new ClassifyFilter();
		XMLDocumentFilter[] filters = { classifyFilter };
		parser.setProperty("http://cyberneko.org/html/properties/filters",
				filters);

		XMLInputSource xmlInputSource = new XMLInputSource(null, null, null,
				new StringReader((String) ctx.getContent()), null);
		try {
			parser.parse(xmlInputSource);
		} catch (XNIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		org.w3c.dom.Document doc = parser.getDocument();
		Document dom4jDoc = dr.read(doc);
		ctx.setContent(dom4jDoc);
		return AttributeChain.EXTRACTXPATH_FILTER;
	}
	
	private void repS(WebContext ctx)
	{
		String content = (String) ctx.getContent();
		if(content.indexOf("<title>") > -1
				&& content.indexOf("</title>") > -1)
		{
			int i = content.indexOf("<title>");
			int j = content.indexOf("</title>");
			String oldStr = content.substring(i + 7, j);
			if(oldStr.indexOf("'") > -1){
				String newStr = oldStr.replaceAll("'", "");
				content  = content.replace(oldStr, newStr);
				ctx.setContent(content);
			}else if(oldStr.indexOf("\"") > -1)
			{
				/**
				 * 针对title里有“"”的情况
				 */
				String newStr = oldStr.replaceAll("\"", "");
				content  = content.replace(oldStr, newStr);
				ctx.setContent(content);
			}
		}
	}
}
